package com.dai.bookkeeping.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dai.bookkeeping.R;
import com.dai.bookkeeping.db.ChartItemBean;
import com.dai.bookkeeping.utils.FloatUtils;

import org.w3c.dom.Text;

import java.util.List;

/**
 * 账单详情页面，listView的适配器
 */
public class ChartItemAdapter extends BaseAdapter {
    Context context;
    List<ChartItemBean> mDatas;
LayoutInflater inflater;
    public ChartItemAdapter(Context context, List<ChartItemBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertview, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertview==null){
            convertview=inflater.inflate(R.layout.item_chartfrag_lv,parent,false);
            holder=new ViewHolder(convertview);
            convertview.setTag(holder);
        }else{
            holder=(ViewHolder) convertview.getTag();
        }
        //获取显示内容
        ChartItemBean bean =mDatas.get(i);
        holder.iv.setImageResource(bean.getsImageId());
        holder.typeTv.setText(bean.getType());
        float ratio=bean.getRatio();
        String pert = FloatUtils.ratioToPercent(ratio);
        holder.ratioTv.setText(pert);
        holder.totalTv.setText("￥"+bean.getTotalMoney());
        return convertview;
    }
//    ViewHolder通常出现在适配器里，为的是listview滚动的时候快速设置值，而不必每次都重新创建很多对象，从而提升性能
    class ViewHolder{
        TextView typeTv,ratioTv,totalTv;
        ImageView iv;
        public ViewHolder(View view){
            typeTv=view.findViewById(R.id.item_chartfrag_tv_type);
            ratioTv=view.findViewById(R.id.item_chartfrag_tv_pert);
            totalTv=view.findViewById(R.id.item_chartfrag_tv_sum);
            iv=view.findViewById(R.id.item_chartfrag_iv);
        }
}
}
